/**
 * 
 */
package com.muniuliuma.screen;


import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.DialogPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;

import com.muniuliuma.R;
import com.muniuliuma.basecontrol.BasePreferenceActivity;
import com.muniuliuma.basecontrol.SharedPreferenceData;

/**
 * @author liuzhaofeng
 * 
 */
public class MenuActivity extends BasePreferenceActivity {

	private PreferenceScreen mBackPref;
	private ListPreference mPOIMarkDisplayPref;
	private CheckBoxPreference mDisplayTrafficePref;
	private DialogPreference mClearLocDataPref;
	private EditTextPreference mMIDCodePref;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.STR_0014);
		findViews();
		setListeners();
	}
	
	/**
	 * find Preference views
	 */
	private void findViews() {
		mBackPref = addPreferenceScreen(R.string.STR_0008);
		mPOIMarkDisplayPref = addListPreference(R.string.STR_0016, R.array.POIMarkDisplay_list_preference, R.array.POIMarkDisplayvalues_list_preference, SharedPreferenceData.KEY_POI_MARK_DISPLAY, R.string.STR_0012);
		mDisplayTrafficePref = addCheckBoxPreference(R.string.STR_0019, R.string.STR_0020, SharedPreferenceData.KEY_DISPLAY_TRAFFICE, false);
		mClearLocDataPref = addDialogPreference(R.string.STR_0026, R.string.STR_0026, R.string.MSG_0001);
		mMIDCodePref = addEditTextPreference(R.string.STR_0027, R.string.STR_0027, R.string.MSG_0002, SharedPreferenceData.KEY_MID_CODE);
	}

	/**
	 * set Preference listeners
	 */
	private void setListeners() {
		mBackPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				backActivity();
				return true;
			}});
		
		mPOIMarkDisplayPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				String summary = getEntry(newValue.toString(), R.array.POIMarkDisplay_list_preference, R.array.POIMarkDisplayvalues_list_preference);
				preference.setSummary(summary);
				
				SharedPreferenceData.setValue(SharedPreferenceData.KEY_POI_MARK_DISPLAY, newValue.toString());
				return true;
			}});
		mDisplayTrafficePref.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference preference) {
				boolean isChecked = ((CheckBoxPreference)preference).isChecked();
				SharedPreferenceData.setValue(SharedPreferenceData.KEY_DISPLAY_TRAFFICE, isChecked);
				return false;
			}});
		
		setDlgPrefPositiveBtn(mClearLocDataPref, android.R.string.ok, new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}});
		mMIDCodePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference preference,
					Object newValue) {
				preference.setSummary(newValue.toString());
				SharedPreferenceData.setValue(SharedPreferenceData.KEY_MID_CODE, newValue.toString());
				return true;
			}});
	}

}
